package com.edu.edu.day25_homework.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.edu.edu.day25_homework.R;
import com.edu.edu.day25_homework.adapter.MyPagerAdapter;
import com.edu.edu.day25_homework.fragment.FragmentFactory;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

//    static {
//
//    }


    private RadioGroup mRadio;
    private ViewPager mVp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        toolBar();
        initDrop();
        initView();
        initData();

    }
    //创建菜单

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.main_menu,menu);
//
//        //得到menu某个菜单项
//        MenuItem item = menu.findItem(R.id.menu_search);
//        //如果是原生的activity
////        SearchView actionView = (SearchView) item.getActionView();
//        SearchView actionView = (SearchView) MenuItemCompat.getActionView(item);
//        //Search的文本监听方法
//        actionView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
//            //当查询文本被提交的时候调用方法
//            @Override
//            public boolean onQueryTextSubmit(String query) {
//                Log.i("TAG","query = " + query);
//                return false;
//            }
//            //查询文本改变的时候调用
//            @Override
//            public boolean onQueryTextChange(String s) {
//                return false;
//            }
//        });
//        return super.onCreateOptionsMenu(menu);
//    }

    private void toolBar() {
        final DrawerLayout mDrawer = (DrawerLayout) findViewById(R.id.main_drawer);
        Toolbar toolbar = (Toolbar) findViewById(R.id.main_tool);
        toolbar.inflateMenu(R.menu.main_menu);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,mDrawer,toolbar,R.string.open,R.string.close);
        toggle.syncState();
        mDrawer.addDrawerListener(toggle);
//        assert mDrawer != null;
//        assert toolbar != null;
//        toolbar.setNavigationOnClickListener();
//
//        new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                //判断抽屉是否打开
//                if (!mDrawer.isDrawerOpen(Gravity.LEFT)) {
//                    //打开抽屉
//                    mDrawer.openDrawer(Gravity.LEFT);
//                } else {
//                    mDrawer.closeDrawer(Gravity.LEFT);
//                }
//            }
//        }
    }

    private void initData() {

        List<Fragment> fragments = new ArrayList<>();
        assert mRadio != null;
        for (int i = 0; i < mRadio.getChildCount(); i++) {
            Fragment fragment = FragmentFactory.createFragment(i);
            fragments.add(fragment);
        }

        assert mVp != null;
        MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager(),fragments);
        mVp.setAdapter(adapter);

        mVp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                ((RadioButton)mRadio.getChildAt(position)).setChecked(true);
            }
        });

        mRadio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checked) {
                for (int i = 0; i < mRadio.getChildCount(); i++) {
                    if(mRadio.getChildAt(i).getId() == checked){
                        mVp.setCurrentItem(i,false);
                    }
                }
            }
        });
    }

    private void initView() {
        mVp = (ViewPager) findViewById(R.id.main_vp);
        mRadio = (RadioGroup) findViewById(R.id.movie_radio);
    }

    private void initDrop(){
        SharedPreferences sp = getSharedPreferences("check", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        boolean isFirst = sp.getBoolean("isFirst",true);
        if(isFirst){
            editor.putBoolean("isFirst",false);
            editor.commit();
            Intent intent = new Intent(this,WelcomeActivity.class);
            startActivity(intent);
        }else{
            Toast.makeText(MainActivity.this,"欢迎来到主界面",Toast.LENGTH_LONG).show();
        }
    }
}
